#====# Appendix A: Describing repeated FCPA actions #====#

# Load libraries and set defaults ----
library(survival)
library(ggfortify)
library(tidyverse)
library(tidylog, warn.conflicts = FALSE)
source("aux/plot_theme.R")

# Import data ----
fcpa <- read_rds("data/FCPA_enf.rds") # FCPA enforcement data

# Figure A.1: Number of distinct FCPA matters per firm ----
fcpa_agg <- fcpa %>%
  group_by(conm, fcpa_matter_id) %>%
  arrange(conm, fcpa_matter_id, date) %>%
  mutate(duration_matter = as.numeric(max(as.Date(date)) - min(as.Date(date))) %/% 365) %>%
  group_by(conm) %>%
  reframe(tot_actions = length(unique(fcpa_matter_id)),
          tot_enforcement = length(unique(fcpa_matter_id[fcpa_action_type == "enforcement"])),
          tot_investigation = length(unique(fcpa_matter_id[fcpa_action_type == "investigation"])),
          latest_year = max(year(date), na.rm = TRUE),
          earliest_year = min(year(date), na.rm = TRUE),
          no_year = length(unique(year(date))),
          max_duration_matter = max(duration_matter, na.rm = TRUE),
          min_duration_matter = min(duration_matter, na.rm = TRUE))

prop.table(table(fcpa_agg$tot_actions > 1))
# 15% of the FCPA targets experienced repeated actions

fcpa_agg %>%
  ggplot(aes(x = tot_actions)) +
  geom_bar() +
  xlab("Number of distinct FCPA matters per firm") +
  ylab("Count of past FCPA target firms") +
  scale_y_continuous(sec.axis = sec_axis(transform = ~.x/nrow(fcpa_agg),
                                         name = "Percentage of total FCPA target firms",
                                         labels = scales::label_percent()))
ggsave("plots/figure_A1.pdf", width = 5, height = 4)

# Figure A.2: Number of distinct years of public involvement in FCPA matters ----
prop.table(table(fcpa_agg$no_year > 1)) 
# 44% of the FCPA targets have been under enforcement for more than a year

fcpa_agg %>%
  ggplot(aes(x = no_year)) +
  geom_bar() +
  xlab("Number of distinct years of\npublic involvement in an FCPA matter") +
  ylab("Count of past FCPA target firms") +
  scale_y_continuous(sec.axis = sec_axis(transform = ~.x/nrow(fcpa_agg),
                                         name = "Percentage of total FCPA target firms",
                                         labels = scales::label_percent(),
                                         breaks = seq(0, 0.5, 0.25)))
ggsave("plots/figure_A2.pdf", width = 5, height = 4)

# which firms were the longest under FCPA matters?
fcpa %>%
  filter(conm == unique(fcpa_agg$conm[fcpa_agg$no_year == 7])) %>%
  arrange(conm, date) %>%
  group_by(conm) %>%
  mutate(n = length(unique(fcpa_matter_id))) %>%
  print(n = Inf)
# ABB Ltd., 7 years for 4 different matters

fcpa %>%
  filter(conm %in% unique(fcpa_agg$conm[fcpa_agg$no_year == 6])) %>%
  arrange(conm, date) %>%
  group_by(conm) %>%
  mutate(n = length(unique(fcpa_matter_id))) %>%
  arrange(-n) %>%
  print(n = Inf)
# ENI (6 years, 5 different matters) and Pfizer (6 years, 4 different matters)

fcpa %>%
  filter(conm %in% unique(fcpa_agg$conm[fcpa_agg$no_year == 5])) %>%
  arrange(conm, date) %>%
  group_by(conm) %>%
  mutate(n = length(unique(fcpa_matter_id))) %>%
  arrange(-n) %>%
  print(n = Inf)
# Halliburton (5 years, 3 matters), Johnson&Johnson (5 years, 2 matters)

# Figure A.3: Survival rate of past FCPA targets to a follow-up FCPA action ----
fcpa_surv <- fcpa %>%
  group_by(conm) %>%
  arrange(conm, date) %>%
  select(conm, date) %>%
  distinct() %>%
  mutate(date = as.Date(date),
         next_date = lead(date, n = 1L),
         status = case_when(is.na(next_date) ~ 0,
                            TRUE ~ 1),
         next_date = case_when(is.na(next_date) ~ as.Date("2025-02-10"),
                               TRUE ~ next_date),
         diff = interval(date, next_date) %/% months(1)) %>%
  ungroup()

fcpa_surv %>%
  arrange(-diff)

surv <- survfit(Surv(diff, status) ~ 1, data = fcpa_surv)
summary(surv, times = c(60, 120, 360))

p <- autoplot(surv)
p$data %>%
  ggplot(aes(x = time, y = surv)) +
  geom_line() +
  geom_point(shape = 4) +
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = .4) +
  scale_y_continuous("Percentage of FCPA targets that have not yet\nexperienced a follow-up FCPA action",
                     labels = ~paste0(100*.x, "%")) +
  scale_x_continuous("Survival time between an FCPA action and the next (in years)",
                     labels = ~round(.x/12, 0),
                     breaks = seq(0, 480, 120))
ggsave("plots/figure_A3.pdf", width = 6, height = 4)

#====# The End #====#